package swarm // Package swarm import "xemo.top/docker/docker/api/types/swarm"
import "time"

// InitRequest is the request used to init a swarm
type InitRequest struct {
	ListenAddr       string
	AdvertiseAddr    string
	DataPathAddr     string
	ForceNewCluster  bool
	Spec             Spec
	AutoLockManagers bool
	Availability     NodeAvailability
	DefaultAddrPool  []string
	SubnetSize       uint32
}

// JoinRequest is the request used to join a swarm.
type JoinRequest struct {
	ListenAddr    string
	AdvertiseAddr string
	DataPathAddr  string
	RemoteAddrs   []string
	JoinToken     string
	Availability  NodeAvailability
}

// Spec represents the spec of a swarm
type Spec struct {
	Annotations
	Orchestration    OrchestrationConfig `json:",omitempty"`
	Raft             RaftConfig          `json:",omitempty"`
	Dispatcher       DispatcherConfig    `json:",omitempty"`
	CAConfig         CAConfig            `json:",omitempty"`
	TaskDefaults     TaskDefaults        `json:",omitempty"`
	EncryptionConfig EncryptionConfig    `json:",omitempty"`
}

// EncryptionConfig 控制数据和密钥的静态加密。
// EncryptionConfig controls at-rest encryption of data and keys.
type EncryptionConfig struct {
	// AutoLockManagers 指定管理器TLS密钥和raft数据是否应在静态时加密，以便必须在管理器节点再次启动之前解锁它们。
	// AutoLockManagers specifies whether or not managers TLS keys and raft data should be encrypted at rest in such a way that they must be unlocked before the manager node starts up again.
	AutoLockManagers bool
}

// TaskDefaults 使用默认值参数化集群级任务创建。
// TaskDefaults parameterizes cluster-level task creation with default values.
type TaskDefaults struct {
	// LogDriver 如果服务未指定，则选择要用于在orchestrator中创建的任务的日志驱动程序
	// LogDriver selects the log driver to use for tasks created in the orchestrator if unspecified by a service
	//
	// 更新此值只会影响新任务。旧任务将继续使用以前配置的日志驱动程序，直到重新创建。
	// Updating this value will only have an affect on new tasks.Old tasks will continue use their previously configured log driver until recreated.
	LogDriver *Driver `json:",omitempty"`
}

// CAConfig represents CA configuration
type CAConfig struct {
	// NodeCertExpiry 是否应颁发持续时间证书
	// NodeCertExpiry is the duration certificates should be issued for
	NodeCertExpiry time.Duration `json:",omitempty"`
	// ExternalCAs 是管理器节点将向其发出节点证书的证书签名请求的ca的列表
	// ExternalCAs is a list of CAs to which a manager node will make certificate signing requests for node certificates
	ExternalCAs []*ExternalCA `json:",omitempty"`
}

// ExternalCA 定义集群要使用的外部CA
// ExternalCA defines external CA to be used by the cluster
type ExternalCA struct {
	// Protocol 是此外部CA使用的协议
	// Protocol is the protocol used by this external CA
	Protocol ExternalCAProtocol
}

// ExternalCAProtocol represents type of external CA.
type ExternalCAProtocol string

// ExternalCAProtocolCFSSL CFSSL
const ExternalCAProtocolCFSSL ExternalCAProtocol = "cfssl"

// DispatcherConfig 表示调度程序配置
// DispatcherConfig represents dispatcher configuration
type DispatcherConfig struct {
	// HeartbeatPeriod 定义代理向dispatcher发送心跳的频率
	// HeartbeatPeriod defines how often agent should send heartbeats to dispatcher
	HeartbeatPeriod time.Duration `json:",omitempty"`
}

// OrchestrationConfig represents orchestration configuration
type OrchestrationConfig struct {
	// TaskHistoryRetentionLimit 是每个实例或节点要保留的历史任务数。如果为负，则从不删除已完成或失败的任务。
	// TaskHistoryRetentionLimit is the number of historic tasks to keep per instance or node. If negative ,never remove completed or failed tasks.
	TaskHistoryRetentionLimit *int64 `json:",omitempty"`
}

// RaftConfig represents raft configuration
type RaftConfig struct {
	// SnapshotInterval 是快照之间的日志条目数
	// SnapshotInterval is the number of log entries between snapshots
	SnapshotInterval uint64 `json:",omitempty"`
	// KeepOldSnapshots 是要保留的当前快照之外的快照数。
	// KeepOldSnapshots is the number of snapshots to keep beyond the current snapshot.
	KeepOldSnapshots *uint64 `json:",omitempty"`
	//LogEntriesForSlowFollowers 是创建快照后为同步慢速关注者而要保留的日志条目数。
	//LogEntriesForSlowFollowers is the number of log entries to keep around to sync up slow followers after a snapshot is created.
	LogEntriesForSlowFollowers uint64 `json:",omitempty"`
	// ElectionTick 是追随者在成为候选人并开始选举之前等待领导者消息的滴答次数。
	// ElectionTick is the number of ticks that a follower will wait for a message from the leader before becoming a candidate and starting an election.
	// ElectionTick 必须大于HeartbeatTick。
	// ElectionTick must be greater than HeartbeatTick.
	//
	// tick当前默认为一秒钟，因此这些当前直接转换为秒，但不能保证
	// A tick currently defaults to one second, so these translate directly to seconds currently,but this is NOT guaranteed
	ElectionTick int
	// HeartbeatTick 是心跳之间的滴答声数。每次心跳滴答声，领导者都会向追随者发送心跳。
	// HeartbeatTick is the number of ticks between heartbeats.Every HeartbeatTick ticks,the leader will send a heartbeat to the followers.
	//
	// tick当前默认为一秒钟，因此这些当前直接转换为秒，但这不能保证。
	// A tick currently defaults to one second,so these translate directly to seconds currently,but this is NOT guaranteed.
	HeartbeatTick int
}

// UnlockRequest 请求是否用于解锁swarm
// UnlockRequest is the request used to unlock a swarm
type UnlockRequest struct {
	// UnlockKey 解锁密钥是否为ASCII装甲格式
	// UnlockKey is the unlock key in ASCII-armored format
	UnlockKey string
}

// Swarm represents a swarm
type Swarm struct {
	ClusterInfo
	JoinTokens JoinTokens
}

// JoinTokens 包含worker和manager需要加入swarm的令牌。
// JoinTokens contains the tokens workers and managers need to join the swarm.
type JoinTokens struct {
	// Worker 是worker可以用来加入swarm的加入令牌。
	// Worker is the join token workers may use to join the swarm.
	Worker string
	// Manager 是加入令牌管理器可以用来加入群
	// Manager is the join token managers may use to join the swarm
	Manager string
}

// ClusterInfo 表示有关在 “info” 中输出的集群的信息，它包含与 “Swarm” 相同的信息，但没有JoinTokens
// ClusterInfo represents info about the cluster for outputting in "info" it contains the same information as "Swarm",but without the JoinTokens
type ClusterInfo struct {
	ID string
	Meta
	Spec                   Spec
	TLSInfo                TLSInfo
	RootRotationInProgress bool
	DefaultAddrPool        []string
	SubnetSize             uint32
	DataPathPort           uint32
}

// UpdateFlags contains flags for SwarmUpdate
type UpdateFlags struct {
	RotateWorkerToken      bool
	RotateManagerToken     bool
	RotateManagerUnlockKey bool
}
